<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 回收物管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>类别信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-circle-plus" @click="addVisible = true">添加类别</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="name" label="类别名称" show-overflow-tooltip=true width="100"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip=true width="200"></el-table-column>
                <el-table-column prop="createTime" label="更新时间" show-overflow-tooltip=true width="200"></el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 详情弹出框 -->
        <el-dialog title="添加类别" v-model="addVisible" width="50%">
            <el-form ref="formRef" :rules="rules" :model="categoryForm" label-width="80px">
                <el-form-item label="类别名称" prop="name">
                    <el-input v-model="categoryForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">添加</el-button>
                    <el-button @click="onReset">清空</el-button>
                    <el-button @click="addVisible = false">关 闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import { onActivated, ref, reactive, inject } from "vue";
import { ElMessage } from "element-plus";
import {getCategoryList, addCategory, addBill} from "../../api";

export default {
    name: "category",
    setup() {
        const query = reactive({
            pageNum: 1,
            pageSize: 10,
            total: 0,
            name: null,
        });

        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        const addVisible = ref(false);

        const rules = {
            name: [
                { required: true, message: "请输入类别名称", trigger: "blur" },
            ]
        };
        const formRef = ref(null);
        let categoryForm = reactive({
            name: '',
        });

        // 提交
        const onSubmit = () => {
            // 表单校验
            formRef.value.validate((valid) => {
                if (valid) {
                    addCategory(categoryForm).then(res => {
                        if (res.code === 200) {
                            onReset();
                            ElMessage.success('添加成功');
                        } else {
                            console.log("[添加回收物类别] 失败: " + res.message);
                            ElMessage.error('服务器繁忙，请稍后重试');
                        }
                        addVisible.value = false;
                        reload();
                    })
                } else {
                    return false;
                }
            });
        };

        // 重置
        const onReset = () => {
            formRef.value.resetFields();
        };

        // 获取表格数据
        const getData = () => {
            getCategoryList(query).then((res) => {
                tableData.value = res.data.content;
                query.total = res.data.count;
            });
        };
        getData();

        // 查询操作
        const handleSearch = () => {
            query.pageNum = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.pageNum = val;
            getData();
        };

        // 表格详情弹窗
        const handleAdd = (index, row) => {
            console.log(row);
            addVisible.value = true;
        };

        onActivated(() => {
            console.log("Category onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            addVisible,
            rules,
            categoryForm,
            formRef,
            handleSearch,
            handlePageChange,
            handleAdd,
            onSubmit,
            onReset
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
